package com.gamecollection.snake.view;

import com.gamecollection.mvc.View;
import com.gamecollection.snake.model.SnakeModel;
import com.gamecollection.snake.utils.GameUtils;

import java.awt.*;

/**
 * 贪吃蛇游戏视图
 */
public class SnakeView implements View {
    
    private SnakeModel model;
    private int width = 800;
    private int height = 600;
    
    public SnakeView(SnakeModel model) {
        this.model = model;
    }
    
    @Override
    public void initialize() {
        // 视图初始化
    }
    
    @Override
    public void render(Graphics g) {
        // 绘制灰色背景
        g.setColor(Color.gray);
        g.fillRect(0, 0, width, height);
        
        // 绘制网格线
        g.setColor(Color.black);
        for (int i = 0; i <= 20; i++) {
            // 横线
            g.drawLine(0, i * 30, 600, i * 30);
            // 竖线
            g.drawLine(i * 30, 0, i * 30, 600);
        }
        
        // 绘制蛇身体
        for (int i = model.getBodyObjList().size() - 1; i >= 0; i--) {
            model.getBodyObjList().get(i).paintSelf(g);
        }
        
        // 绘制蛇头
        model.getHeadObj().paintSelf(g);
        
        // 绘制食物
        model.getFoodObj().paintSelf(g);
        
        // 绘制关卡信息
        GameUtils.drawWord(g, "第" + model.getLevel() + "关", Color.orange, 40, 650, 260);
        
        // 绘制分数
        GameUtils.drawWord(g, model.getScore() + " 分", Color.BLUE, 50, 650, 330);
        
        // 绘制提示语
        drawPrompt(g);
    }
    
    @Override
    public void update() {
        // 视图更新
    }
    
    @Override
    public void show() {
        // 显示视图
    }
    
    @Override
    public void hide() {
        // 隐藏视图
    }
    
    /**
     * 绘制提示语
     */
    private void drawPrompt(Graphics g) {
        int state = model.getGameState();
        
        switch (state) {
            case SnakeModel.STATE_NOT_STARTED:
                g.setColor(Color.gray);
                g.fillRect(120, 240, 400, 70);
                GameUtils.drawWord(g, "按下空格键开始游戏", Color.yellow, 35, 150, 290);
                break;
            case SnakeModel.STATE_PAUSED:
                g.setColor(Color.gray);
                g.fillRect(120, 240, 400, 70);
                GameUtils.drawWord(g, "按下空格键继续游戏", Color.yellow, 35, 150, 290);
                break;
            case SnakeModel.STATE_GAME_OVER:
                g.setColor(Color.gray);
                g.fillRect(120, 240, 400, 70);
                GameUtils.drawWord(g, "失败,按空格重新开始", Color.red, 35, 150, 290);
                break;
            case SnakeModel.STATE_LEVEL_COMPLETE:
                g.setColor(Color.gray);
                g.fillRect(120, 240, 400, 70);
                if (model.getLevel() == 3) {
                    GameUtils.drawWord(g, "达成条件,游戏通关", Color.green, 35, 150, 290);
                } else {
                    GameUtils.drawWord(g, "达成条件,空格下一关", Color.green, 35, 150, 290);
                }
                break;
        }
    }
    
    public int getWidth() {
        return width;
    }
    
    public int getHeight() {
        return height;
    }
} 